﻿using HB.HttpServer.Model;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace HB.HttpServer
{
    public static class ResponseHelper
    {
        /// <summary>
        /// 返回json字符串
        /// </summary>
        /// <param name="ae"></param>
        /// <param name="result"></param>
        public static void ReturnJson(this ActionExecutingData ae, string json)
        {
            ReturnString(ae, "application/json;charset=utf-8", json);
        }
        /// <summary>
        /// 返回html
        /// </summary>
        /// <param name="ae"></param>
        /// <param name="html"></param>
        public static void ReturnHtml(this ActionExecutingData ae, string html)
        {
            ReturnString(ae, "text/html;charset:utf-8", html);
        }
        static void ReturnString(ActionExecutingData ae, string contentType, string str)
        {
            byte[] b = Encoding.UTF8.GetBytes(str);
            ae.HttpContex.Response.ContentType = contentType;
            ae.HttpContex.Response.OutputStream.Write(b, 0, b.Length);
            ae.HttpContex.Response.Close();
            ae.CanContinue = false;
        }
        /// <summary>
        /// 返回状态码，无数据
        /// </summary>
        /// <param name="ae"></param>
        /// <param name="code"></param>
        public static void ReturnCode(this ActionExecutingData ae, HttpStatusCode code, string statusDescription = "")
        {
            ae.HttpContex.Response.StatusCode = (int)code;
            ae.HttpContex.Response.StatusDescription = statusDescription;
            ae.HttpContex.Response.Close();
            ae.CanContinue = false;
        }
        static Dictionary<string, string> DicContentType = new Dictionary<string, string>()
        {
            {".js","application/x-javascript;charset=utf-8" } ,
            {".bmp","application/x-bmp" },
            {".doc","application/msword;charset=utf-8" } ,
            {".gif","image/gif" } ,
            {".html","text/html;charset:utf-8" } ,
            {".ico","image/x-icon" } ,
            {".jpeg","image/jpeg" },
            {".jsp","text/html;charset:utf-8" },
            {".apk","application/vnd.android.package-archive" },
            {".avi","video/avi" },
            {".css","text/css;charset:utf-8" },
            {".dll","application/x-msdownload" } ,
            {".htm","text/html;charset:utf-8" },
            {".jpg","image/jpeg" },
            {".png","image/png" } ,
            {".ppt","application/vnd.ms-powerpoint;charset:utf-8" } ,
            {".pdf","application/pdf;charset:utf-8" }  ,
        };
        /// <summary>
        /// 返回文件
        /// </summary>
        /// <param name="ae"></param>
        /// <param name="file"></param>
        public static void ReturnFile(this ActionExecutingData ae, string file)
        {
            var ex = Path.GetExtension(file);
            var contentType = "application/octet-stream";
            if (DicContentType.ContainsKey(ex))
            {
                contentType = DicContentType[ex];
            }
            ae.HttpContex.Response.ContentType = contentType;
            ae.HttpContex.Response.Headers.Set("Content-Disposition", $"attachment; filename={ System.Web.HttpUtility.UrlEncode ( Path.GetFileName(file))}");
            using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                byte[] data = new byte[1024];
                int len = 0;
                while ((len = fs.Read(data, 0, 1024)) > 0)
                {
                    ae.HttpContex.Response.OutputStream.Write(data, 0, len);
                }
                ae.HttpContex.Response.Close();
            }
            ae.CanContinue = false;
        }
    }
}
